package test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

class Team {
    private String name;
    int played;
    int won;
    int drawn;
    int lost;
    int goalsFor;
    int goalsAgainst;
    int points;

    public Team(String name) {
        this.name = name;
        this.played = 0;
        this.won = 0;
        this.drawn = 0;
        this.lost = 0;
        this.goalsFor = 0;
        this.goalsAgainst = 0;
        this.points = 0;
    }

    public String getName() {
        return name;
    }

    public int getPlayed() {
        return played;
    }

    public int getWon() {
        return won;
    }

    public int getDrawn() {
        return drawn;
    }

    public int getLost() {
        return lost;
    }

    public int getGoalsFor() {
        return goalsFor;
    }

    public int getGoalsAgainst() {
        return goalsAgainst;
    }

    public int getPoints() {
        return points;
    }

    public void updateStats(int goalsFor, int goalsAgainst) {
        this.played++;
        this.goalsFor += goalsFor;
        this.goalsAgainst += goalsAgainst;
        if (goalsFor > goalsAgainst) {
            this.won++;
            this.points += 3;
        } else if (goalsFor == goalsAgainst) {
            this.drawn++;
            this.points += 1;
        } else {
            this.lost++;
        }
    }
}

public class test {
    public static void main(String[] args) {
        List<Team> teams = readTeamsFromFile("E:\\毕业设计\\zwmwork\\TextJava\\src\\test\\league.txt");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入比赛结果（格式：主队名，客队名，主队得分，客队得分，输入end结束）：");

        while (true) {
            String input = scanner.nextLine().trim();
            if ("end".equals(input)) {
                break;
            }
            String[] result = input.split(",");
            if (result.length!= 4) {
                System.out.println("输入格式不正确，请重新输入！");
                continue;
            }
            String homeTeamName = result[0].trim();
            String awayTeamName = result[1].trim();
            int homeGoals;
            int awayGoals;
            try {
                homeGoals = Integer.parseInt(result[2].trim());
                awayGoals = Integer.parseInt(result[3].trim());
            } catch (NumberFormatException e) {
                System.out.println("进球数输入格式错误，请重新输入！");
                continue;
            }

            Team homeTeam = null;
            Team awayTeam = null;
            for (Team team : teams) {
                if (team.getName().equals(homeTeamName)) {
                    homeTeam = team;
                } else if (team.getName().equals(awayTeamName)) {
                    awayTeam = team;
                }
                if (homeTeam!= null && awayTeam!= null) {
                    break;
                }
            }

            if (homeTeam == null || awayTeam == null) {
                System.out.println("队伍名称输入有误，请重新输入！");
                continue;
            }

            homeTeam.updateStats(homeGoals, awayGoals);
            awayTeam.updateStats(awayGoals, homeGoals);
        }

        rankTeams(teams);
        displayTeams(teams);
        writeTeamsToFile(teams, "league.txt");

        scanner.close();
    }

    private static List<Team> readTeamsFromFile(String fileName) {
        List<Team> teams = new ArrayList<>();
        try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) {
            String line;
            while ((line = reader.readLine())!= null) {
                String[] parts = line.split(",");
                if (parts.length!= 7) {
                    continue;
                }
                String teamName = parts[0].trim();
                Team team = new Team(teamName);
                team.played = Integer.parseInt(parts[1].trim());
                team.won = Integer.parseInt(parts[2].trim());
                team.drawn = Integer.parseInt(parts[3].trim());
                team.lost = Integer.parseInt(parts[4].trim());
                team.goalsFor = Integer.parseInt(parts[5].trim());
                team.goalsAgainst = Integer.parseInt(parts[6].trim());
                team.points = team.won * 3 + team.drawn;
                teams.add(team);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return teams;
    }

    private static void rankTeams(List<Team> teams) {
        teams.sort((t1, t2) -> {
            if (t1.getPoints()!= t2.getPoints()) {
                return Integer.compare(t2.getPoints(), t1.getPoints());
            }
            int goalDifference1 = t1.getGoalsFor() - t1.getGoalsAgainst();
            int goalDifference2 = t2.getGoalsFor() - t2.getGoalsAgainst();
            if (goalDifference1!= goalDifference2) {
                return Integer.compare(goalDifference2, goalDifference1);
            }
            return Integer.compare(t2.getGoalsFor(), t1.getGoalsFor());
        });
    }

    private static void displayTeams(List<Team> teams) {
        System.out.println("球队排名情况：");
        System.out.println("--------------------------------------------------");
        System.out.printf("%-15s %-10s %-10s %-10s %-10s %-10s %-10s %-10s\n", "队名", "已赛场数", "胜场数", "平场数", "负场数", "进球数", "失球数", "积分");
        System.out.println("--------------------------------------------------");
        int rank = 1;
        for (Team team : teams) {
            System.out.printf("%-15s %-10d %-10d %-10d %-10d %-10d %-10d %-10d\n", team.getName(), team.getPlayed(), team.getWon(), team.getDrawn(), team.getLost(), team.getGoalsFor(), team.getGoalsAgainst(), team.getPoints());
            rank++;
        }
    }

    private static void writeTeamsToFile(List<Team> teams, String fileName) {
        try (FileWriter writer = new FileWriter(new File(fileName))) {
            for (Team team : teams) {
                writer.write(String.format("%s,%d,%d,%d,%d,%d,%d\n", team.getName(), team.getPlayed(), team.getWon(), team.getDrawn(), team.getLost(), team.getGoalsFor(), team.getGoalsAgainst()));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}